﻿<%@ Page Title="转季补货系数" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<link href="../assets/css/shelf/base.css" rel="stylesheet">
<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<link href="../assets/css/plugins/iCheck/customsmall.css" rel="stylesheet" />

<style>
.system-box .right-side {width: calc(100% - 221px);}
.system-box .right-side .ibox-content {padding: 0;}
.checkCalcRate{
	border: 0;
	width: 100px;
}
.tips {
    line-height: normal;
    color:#bc7b21;
    background:#fffdcd;
    padding:5px;
    border-bottom:1px solid #e5e5e5;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" runat="Server">
    <div class="system-box">
        <div class="left-side ibox-content">
            <div id="using_json"></div>
        </div>
        <div class="right-side">
        	<div class="btns-container"></div>
            <div class="tips"><i class="fa fa-lightbulb-o"></i>选择转季时间点"生成"按钮生成转季时间段,"计算"转季系数,"保存"选中行.</div>
        	<div class="condition">        
                <label>转季时间</label>
				<input type="text" class="form-control layer-date" id="search-start" placeholder="转季开始时间" />
				<label class="to">至</label>
				<input type="text" class="form-control layer-date" id="search-end" placeholder="转季结束时间" />
            </div>
            <div class="right-content-box">
                <div class="ibox-content">
                    <table class="footable table table-stripped table-list">
                        <thead></thead>
                        <tbody id="pageList"></tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" runat="Server">
    <script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
    <script src="../assets/js/plugins/layer/laydate/laydate.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
    <script src="../assets/js/plugins/iCheck/icheckAll.js"></script>

    <script>
        $(document).ready(function () {
            var ModuleID = Y.getUrlParam("ModuleID");
            //获取按钮
			commonUtil.loadBtnList(ModuleID,function(){
			    rScroll();
			});
			
			//初始化搜索日期控件
			var searchstart = {
                elem: "#search-start",
                format: "YYYY-MM-DD",
                min: "2015-12-01",
                max: "2099-12-01",
                istime: true,
                istoday: false,
                choose: function (datas) {
                    searchend.min = datas;
                    searchend.start = datas
                }
            };
            var searchend = {
                elem: "#search-end",
                format: "YYYY-MM-DD",
                min: "2015-12-01",
                max: "2099-12-01",
                istime: true,
                istoday: false,
                choose: function (datas) {
                    searchstart.max = datas
                }
            };
            laydate(searchstart);
            laydate(searchend);

            //用于记录当前选中节点字典id（左侧）
            var did = '-1';
            //用于记录当前选中节点字典id（右侧）
            var rdid = 0;
            //用于记录当前选中节点的名称
            var dName = '';
            //用于记录当前选中节点的是否树节点
            var dIsTree = '';

            //加载左侧列表
            var loadList = function () {
                //调用加载层
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2("DwShop", "GetBasOperationArea", {}, function (data) {
                    var treeData = [];
                    if (!Y.checkNull(data) && data.length > 0) {
                        var children = [];
                        var temp = {}
                        temp.text = '零售分部';
                        temp.data = {
                            OperationAreaID: '-1',
                            OperationAreaName: '零售分部',
                            IsTree: true,
                            IsParent:true,
                        };
                        //json处理
                        data.map(function (item, i) {
                            var te = {}
                            te.text = item.OperationAreaName;
                            te.data = {
                                OperationAreaID: item.OperationAreaID,
                                OperationAreaName: item.OperationAreaName,
                                IsTree: true,
                                IsParent: false,
                            };
                            children.push(te);
                        })
                        temp.state = {opened: true};
                        temp.children = children;
                        treeData.push(temp);
                        //初始化左侧菜单
                        $.jstree.destroy("#using_json");
                        $('#using_json').jstree({
                            core: {
                                data: treeData
                            }
                        }).bind('select_node.jstree', function (event, data) {
                            //绑定的点击事件
                            var node_data = data.node.data;
                            did = node_data.OperationAreaID;
                            dName = node_data.OperationAreaName;
                            dIsTree = node_data.IsTree;
                            dIsParent = node_data.IsParent;
                            //加载右侧列表
                            if (!dIsParent) {
                                loadPageList(did);
                            }
                        });
                    }
                    //关闭加载层
                    layer.closeAll('dialog');
                });
            };
            loadList();

            //处理表头
            if ($('.footable thead').children().size() == 0) {
                tableControl.initThead('.footable', [
                    {
                        trClass: '', children: [
                            { tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" id="checkAllChange" />', tdId: '', tdClass: '' },
                            { tdText: '转季日期', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '后七天时间段(含当天)', c: 2, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '前七天时间段', c: 2, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '后七天总目标', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '前七天总目标', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '转季系数', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                            { tdText: '确认系数', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                        ]
                    }
                ])
                $(".footable input[type='checkbox']").iCheck({ checkboxClass: "icheckbox_square-green" });
            }

            var loadPageList = function (did) {
                //调用加载层
                layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
                Y.API2("DWChangeSeasonRate", "GetDWChangeSeasonRateList", { OperationAreaID:did }, function (data) {
                	layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        var tBody = [];
                        if (!Y.checkNull(data) && data.length > 0) {
                            data.map(function (item, i) {
                                var tempTr = {
                                    trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                    children: [
                                    	{ tdText: '', c: 0, r: 0, tdHtml: '<input type="checkbox" class="checkChange" />', tdId: '', tdClass: '' },
                                        { tdText: item.YearNo, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'yearNo hide' },
                                        { tdText: item.OperationAreaID, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'operationAreaID hide' },
                                        { tdText: item.ChangeDate, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'changeDate' },
                                        { tdText: item.NextBeginDate, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'nextBeginDate' },
                                        { tdText: item.NextEndDate, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'nextEndDate' },
                                        { tdText: item.PastBeginDate, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'pastBeginDate' },
                                        { tdText: item.PastEndDate, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'pastEndDate' },
                                        { tdText: (item.NextTargetAmount || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'nextTargetAmount' },
                                        { tdText: (item.PastTargetAmount || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'pastTargetAmount' },
                                        { tdText: (item.CalcRate || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                        { tdText: '', c: 0, r: 0, tdHtml: '<input class="checkCalcRate" value="' +
                                        (item.CheckCalcRate || ' ') +'" data-old="'+ (item.CheckCalcRate || ' ') +'" />', tdId: '', tdClass: '' },
                                    ]
                                };
                                tBody.push(tempTr);
                            })
                        }
                        tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 9, null);
                        //选择全部插件 2017-05-02 改
                        $(".footable").icheckAll({
                            checkObj: ".checkChange",//表格下面的选择控件对象 选择器
                            checkAllObj: "#checkAllChange",//全选 选择器
                            clickoutBoxCheck: false,//点击行 不选中
                            checkboxMargin: "4px 0 0 0 "//上右下左
                        });
                    }
                });
            };
            
            ////全选 2017-05-02注释
			//$('.footable').on('click','thead #checkAllChange',function(){
			//	if(this.checked)
			//		$('.footable .checkChange').each(function(){this.checked=true;});
			//	else
			//		$('.footable .checkChange').each(function(){this.checked=false;});
			//});
			
            ////单选 2017-05-02注释
			//$('.footable').on('click','.checkChange',function(){
			//	if($('.footable .checkChange').size() == $('.footable .checkChange:checked').size())
			//		$('.footable #checkAllChange').prop("checked", true);
			//	else
			//		$('.footable #checkAllChange').prop("checked", false);
			//});
            
            //生成按钮
            $('.btns-container').on('click','#generateBtn',function(){
            	if(did == '-1'){
            		swal('请选择零售分部');
            		return false;
            	}
            	if($('#search-start').val().trim() == ''){
            		swal('请选择转季开始时间');
            		return false;
            	}
            	if($('#search-end').val().trim() == ''){
            		swal('请选择转季结束时间');
            		return false;
            	}
            	layer.msg('玩命加载中……', { icon: 16, shade: 0.5, time: 0, maxWidth: 200 });
            	Y.API2('DWChangeSeasonRate','CreatDWChangeSeasonRate',{
            		OperationAreaID: did,
            		StartDate: $('#search-start').val().trim(),
            		EndDate: $('#search-end').val().trim(),
            	},function(data){
            		if(data.Success == false){
            			layer.closeAll('dialog');
            			swal(data.Message,'','error');
            		}else{
            			loadPageList(did);
            		}
            	})
            });
            
            //计算按钮
            $('.btns-container').on('click','#calculateBtn',function(){
            	if($('#pageList .checkChange:checked').size() == 0){
		    		swal('请先选择转季日期');
		    		return false;
		    	}
            	var submitList = [];
            	$('#pageList .checkChange:checked').each(function(){
            		var tr = $(this).parents('tr');
            		submitList.push({
            			NextTargetAmount: tr.find('.nextTargetAmount').text().trim(),
	            		PastTargetAmount: tr.find('.pastTargetAmount').text().trim(),
						YearNo: tr.find('.yearNo').text().trim(),
						OperationAreaID: tr.find('.operationAreaID').text().trim(),
						ChangeDate: tr.find('.changeDate').text().trim(),
						NextBeginDate: tr.find('.nextBeginDate').text().trim(),
						NextEndDate: tr.find('.nextEndDate').text().trim(),
						PastBeginDate: tr.find('.pastBeginDate').text().trim(),
						PastEndDate: tr.find('.pastEndDate').text().trim(),
            		})
            	})
//          	console.log($('#pageList .checkChange:checked').size(),submitList);
            	layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
            	Y.API2('DWChangeSeasonRate','ModifyDWChangeSeasonRate',{
            		list: submitList,
            		OperationAreaName: dName
            	},function(data){
            		layer.closeAll('dialog');
            		if(data.Success == false){
            			swal(data.Message,'','error');
            		}else{
            			loadPageList(did);
            		}
            	})
            });
            
            //限制输入系数
            $('.footable').on('keyup','.checkCalcRate',function(){
            	var tempValue = $(this).val();
				if(tempValue.length > 5)
					tempValue = tempValue.substr(0,5);
				if(isNaN(parseFloat(tempValue))){
					tempValue = $(this).attr('data-old');
				}
				$(this).val(tempValue);
            })
            
            //保存按钮
            $('.btns-container').on('click','#btn-save',function(){
            	if($('#pageList .checkChange:checked').size() == 0){
		    		swal('请先选择转季日期');
		    		return false;
		    	}
            	var submitList = [];
            	$('#pageList .checkChange:checked').each(function(){
            		var tr = $(this).parents('tr');
            		var checkCalcRate = parseFloat(tr.find('.checkCalcRate').val().trim());
            		submitList.push({
            			NextTargetAmount: tr.find('.nextTargetAmount').text().trim(),
	            		PastTargetAmount: tr.find('.pastTargetAmount').text().trim(),
						YearNo: tr.find('.yearNo').text().trim(),
						OperationAreaID: tr.find('.operationAreaID').text().trim(),
						ChangeDate: tr.find('.changeDate').text().trim(),
						NextBeginDate: tr.find('.nextBeginDate').text().trim(),
						NextEndDate: tr.find('.nextEndDate').text().trim(),
						PastBeginDate: tr.find('.pastBeginDate').text().trim(),
						PastEndDate: tr.find('.pastEndDate').text().trim(),
						CheckCalcRate: isNaN(checkCalcRate) ? tr.find('.checkCalcRate').attr('data-old') : checkCalcRate,
            		})
            	})
            	layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
            	Y.API2('DWChangeSeasonRate','UpdateDWChangeSeasonRate',{
            		list: submitList
            	},function(data){
            		layer.closeAll('dialog');
            		if(data.Success == false){
            			swal(data.Message,'','error');
            		}else{
            			loadPageList(did);
            		}
            	})
            });
            
            //删除按钮
            $('.btns-container').on('click','#deleteBtn',function(){
            	if($('#pageList .checkChange:checked').size() == 0){
		    		swal('请先选择转季日期');
		    		return false;
		    	}
            	swal({
					title: "确定要删除吗？",
					type: "warning",
					showCancelButton: true,
					confirmButtonColor: "#DD6B55",
					confirmButtonText: "确定",
					cancelButtonText: "取消",
				}, function(){
					var submitList = [];
	            	$('#pageList .checkChange:checked').each(function(){
	            		var tr = $(this).parents('tr');
	            		submitList.push({
	            			NextTargetAmount: tr.find('.nextTargetAmount').text().trim(),
		            		PastTargetAmount: tr.find('.pastTargetAmount').text().trim(),
							YearNo: tr.find('.yearNo').text().trim(),
							OperationAreaID: tr.find('.operationAreaID').text().trim(),
							ChangeDate: tr.find('.changeDate').text().trim(),
							NextBeginDate: tr.find('.nextBeginDate').text().trim(),
							NextEndDate: tr.find('.nextEndDate').text().trim(),
							PastBeginDate: tr.find('.pastBeginDate').text().trim(),
							PastEndDate: tr.find('.pastEndDate').text().trim(),
	            		})
	            	})
//	            	console.log($('#pageList .checkChange:checked').size(),submitList);
	            	layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
	            	Y.API2('DWChangeSeasonRate','DelDWChangeSeasonRate',{
	            		list: submitList
	            	},function(data){
	            		layer.closeAll('dialog');
	            		if(data.Success == false){
	            			swal(data.Message,'','error');
	            		}else{
	            			loadPageList(did);
	            		}
	            	})
				});
            });
        });
    </script>
</asp:Content>

